<template>
  <div class="app-container customer-div">
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="品类：">
          <el-select
                  clearable
                  v-model="listQuery.goodsName">
            <el-option
                    v-for="item in options2"
                    :key="item.goodsName"
                    :label="item.goodsName"
                    :value="item.goodsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="牌号：">
          <el-select
                  clearable
                  v-model="listQuery.goodsKingId"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入关键词"
                  :remote-method="remoteMethod2"
                  :loading="loading2">
            <el-option
                    v-for="item in options"
                    :key="item.kingId"
                    :label="item.goodsSn"
                    :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库：">
          <el-select v-model="listQuery.warehouseName" filterable placeholder="请选择" clearable>
            <el-option
                    v-for="item in wList"
                    :key="item"
                    :label="item"
                    :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金蝶编号：">
          <el-input v-model="listQuery.kingNumber"></el-input>
        </el-form-item>
        <el-form-item label="是否过滤零可用库存：" label-width="150px" >
            <el-select v-model="listQuery.isFilterEnableNum " clearable>
                <el-option :key="1" label="是" :value="1"></el-option>
                <el-option :key="0" label="否" :value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
          <el-button
                  v-if="exportInventoryQueryReport"
                  :loading="btnloading"
                  @click="downloadExcel()"
                  type="primary">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="margin:20px 0;font-size:14px;">
        <span>合计：</span>
        <span style="padding-right:20px;">可用库存数量 <b>{{enableAllNum}}</b></span>
        <span style="padding-right:20px;">实际库存数量 <b>{{stockAllNum}}</b></span>
        <span style="padding-right:20px;">预计待入库数量 <b>{{unStockInAllNum}}</b></span>
        <span style="padding-right:20px;">销售在订数量 <b>{{unSendAllNum}}</b></span>
        <span style="padding-right:20px;">初步合同预订数量 <b>{{tentativeNum}}</b></span>
        <span style="padding-right:20px;" v-if="isShowtd">询盘量 <b>{{inquiryAllNum}}</b></span>
    </div>

    <!--数据列表-->
    <div class="table-container" style="margin-top:0;">
      <el-table
              v-loading="listLoading"
              :data="tableData"
              border
              style="width: 100%;">
        <el-table-column  label="品类" align="center" prop="goodsName" width="150">
        </el-table-column>
        <el-table-column  label="仓库名称" align="center" prop="warehouseName" width="150">
        </el-table-column>
        <el-table-column  label="牌号"  align="center" width="200">
          <template slot-scope="scope">
              {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column  label="金蝶编号"  align="center" width="120">
          <template slot-scope="scope">
            {{scope.row.kingNumber}}
          </template>
        </el-table-column>
        <el-table-column  label="可用库存数量" align="center" prop="enableNum" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              可用 <el-tooltip class="item" effect="dark" content="实际库存数量+预计待入库数量-销售在订单量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
              <br/>库存数量
            </div>
          </template>
          <template slot-scope="scope">
            {{scope.row.enableNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column min-width="150" class-name="nopadding">
          <template slot="header" slot-scope="scope">
            <div style="text-align: center">
              实际 <el-tooltip class="item" effect="dark" content="仓库在库数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
              <br/>库存数量
            </div>
          </template>
          <template slot-scope="scope">
            <div style="color:red" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}" v-for="(item,index) in scope.row.showList" v-show="item.stockNum > 0" v-if="scope.row.stockList.length > 1">
              <span style="width:94px;display: inline-block;padding-left:15px;padding-right:5px;">{{item.warehouseName}}</span><span>{{item.stockNum}}</span>
            </div>
            <div style="color:red;text-align: center" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}" v-for="(item,index) in scope.row.stockList" v-else>
              <span>{{item.stockNum}}</span>
            </div>
            <div :style="{borderTop:scope.row.showList.length > 0  ? '1px solid #EBEEF5' :'0' }"  v-if="scope.row.stockList.length > 1">
              <span style="width:94px;display: inline-block;font-weight:bold;color:#000;padding-left:15px;">合计</span> <span style="font-weight:bold;">{{ scope.row.stockList | fiflist}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="unStockInNum" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              预计 <el-tooltip class="item" effect="dark" content="未入库采购订单数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
              <br/>待入库数量
            </div>
          </template>
          <template slot-scope="scope">
            {{scope.row.unStockInNum | fifFour}}
          </template>
        </el-table-column>

        <el-table-column  align="center" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              销售 <el-tooltip class="item" effect="dark" content="采购产品线负责人审核过的销售合同未发货数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
              <br/>在订数量
            </div>
          </template>
          <template slot-scope="scope">
              {{scope.row.unSendNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="unStockInNum" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              财务已审核数量
            </div>
          </template>
          <template slot-scope="scope">
              {{scope.row.financeCheckNum}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="unStockInNum" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              财务未审核数量
            </div>
          </template>
          <template slot-scope="scope">
            {{scope.row.financeUnCheckNum}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              初步 <el-tooltip class="item" effect="dark" content="采购员确认过的合同数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
              <br/>合同预订数量
            </div>
          </template>
          <template slot-scope="scope">
            {{scope.row.tentativeNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="120" v-if="isShowtd">
          <template slot="header" slot-scope="scope">
            <div>
              询盘量 <el-tooltip class="item" effect="dark" content="销售首次确认的合同数量（金蝶）" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
            </div>
          </template>
          <template slot-scope="scope">
              {{scope.row.inquiryNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column   align="center" min-width="120">
          <template slot="header" slot-scope="scope">
            <div>
              全款付款量 <el-tooltip class="item" effect="dark" content="合同全款支付未发货数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
            </div>
          </template>
          <template slot-scope="scope">
              {{scope.row.hasPaidNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" v-if="isShowtd">
          <template slot="header" slot-scope="scope">
            <div>
              订金付款量 <el-tooltip class="item" effect="dark" content="合同部分付款未发货数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
            </div>
          </template>
          <template slot-scope="scope">
              {{scope.row.unFinishedPaidNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150">
          <template slot="header" slot-scope="scope">
            <div>
              货到付款量 <el-tooltip class="item" effect="dark" content="合同已审核未付款未发货数量" placement="right">
              <i class="el-icon-question"></i>
            </el-tooltip>
            </div>
          </template>
          <template slot-scope="scope">
            {{scope.row.codPaidNum | fifFour}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" label="最低仓位" v-if="isShowtd">
          <template slot-scope="scope">
            <div v-if="(scope.row.enableNum < scope.row.goodsMinStock) && scope.row.isStockWarning == 1 " style="background: #d58512;color:#fff">
              {{scope.row.goodsMinStock | fifFour}}
            </div>
            <div v-else-if="scope.row.isStockWarning == 1 ">
              {{scope.row.goodsMinStock | fifFour}}
            </div>
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" label="最高仓位" v-if="isShowtd">
          <template slot-scope="scope">
            <div v-if="(scope.row.enableNum > scope.row.goodsMaxStock) && scope.row.isStockWarning == 1 " style="background: red;color:#fff">
              {{scope.row.goodsMaxStock | fifFour}}
            </div>
            <div v-else-if="scope.row.isStockWarning == 1 ">
              {{scope.row.goodsMaxStock | fifFour}}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
<!--    <div class="pagination-container">-->
<!--      <el-pagination-->
<!--              background-->
<!--              @size-change="handleSizeChange"-->
<!--              @current-change="handleCurrentChange"-->
<!--              layout="total, sizes,prev, pager, next,jumper"-->
<!--              :current-page.sync="listQuery.pageNum"-->
<!--              :page-size="listQuery.pageSize"-->
<!--              :page-sizes="[10,15,20]"-->
<!--              :total="count">-->
<!--      </el-pagination>-->
<!--    </div>-->

  </div>
</template>
<script>
  import {warehouseStockList,queryGoodsStock,queryGoodsStockSummary,getGoodsClassList} from '@/api/partner'
  import {goodsList} from '@/api/company'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    goodsKingId:null,
    // warehouseId:null,
    warehouseName:'',
    goodsName:'',
    kingNumber:'',
    isFilterEnableNum:''
  };
  export default {
    name: "index",
    data(){
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        wList:['江苏一号仓','外仓','工厂仓','余达仓','塑通仓'],
        options:[],
        loading2:false,
        enableAllNum:'',
        stockAllNum:'',
        unStockInAllNum :'',
        unSendAllNum :'',
        inquiryAllNum:'',
        btnloading:false,
        isShowtd:false,
        tentativeNum:'',
        options2:[],
        loading3:false
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),
      exportInventoryQueryReport() {
        if (this.roles.indexOf("exportInventoryQueryReport") != -1) {
          return true
        } else {
          return false
        }
      },
    },
    created() {
      this.getList();
      this.getNum();
      this.remoteMethod3();
    },
    filters: {
      formatCreateTime(time) {
        if(time){
          let date = new Date(time);
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        }else{
          return '--'
        }
      },
      fiflist(list){
        let sum = 0;
         list.forEach((el)=>{
            sum = sum + parseFloat(el.stockNum)
         });
         return parseFloat(sum).toFixed(4)
      },
      fifFour(val){
        return parseFloat(val).toFixed(4)
      },
    },
    methods:{
      //获取品类列表
      remoteMethod3() {
          getGoodsClassList().then((res)=>{
            this.options2 = res;
          }).catch(()=>{
          });
      },

      //下载excle
      downloadExcel(){
        let params = JSON.parse(JSON.stringify(this.listQuery));
        params.isExport = 1;
        this.btnloading = true;
        queryGoodsStock(params).then((res)=>{
          this.btnloading = false;
          require.ensure([], () => {
            const { export_json_to_excel } = require('sheet/Export2Excel');
            const tHeader = res.title;
            const filterVal = res.columns;
            const list = res.list;
            const data = this.formatJson(filterVal, list);
            export_json_to_excel(tHeader, data, res.fileName);
          })
        }).catch(()=>{
          this.btnloading = false;
        })
      },

      //下载表格数据格式化
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },
      //获取商品列表
      remoteMethod2(query) {
        if (query !== '') {
          this.loading2 = true;
          goodsList({keywords:query}).then((res)=>{
            this.options = res.list;
            this.loading2 = false;
          }).catch(()=>{
            this.loading2 = false;
          });
        } else {
          this.options = [];
        }
      },


      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
        this.getNum();
      },

      //获取列表
      getList() {
        this.listLoading = true;
        queryGoodsStock(this.listQuery).then(response => {
          this.tableData = [];
          response.forEach((el,index)=>{
            el.showList = [];
            if(el.stockList.length > 1){
              el.showList = el.stockList.filter((ee)=>{
                return ee.stockNum > 0
              });
            }else{
              el.showList = JSON.parse(JSON.stringify(el.stockList))
            }
            this.tableData.push(el)
          });
          this.listLoading = false;
          this.isShowtd = this.listQuery.warehouseName == '' ? true :false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },

      //获取库存总数
      getNum(){
        let data = JSON.parse(JSON.stringify(this.listQuery));
        delete data.pageNum;
        delete data.pageSize;
        queryGoodsStockSummary(data).then((res)=>{
                  this.enableAllNum = res.enableAllNum;
                  this.stockAllNum = res.stockAllNum;
                  this.unStockInAllNum = res.unStockInAllNum;
                  this.unSendAllNum = res.unSendAllNum;
                  this.inquiryAllNum = res.inquiryAllNum;
                  this.tentativeNum = res.tentativeNum
        })
      },
    },
  }
</script>

<style scoped lang="scss">
  .upload-div{
     text-align: center;
     position:relative;
     #upload{
       opacity: 0;
       position: absolute;
       z-index: 1;
       width:154px;
       height:40px
     }
   }
</style>
